package com.booktrade.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.booktrade.dto.BookSearchDTO;
import com.booktrade.entity.Books;
import com.booktrade.entity.BooksTags;
import com.booktrade.entity.Tags;
import com.booktrade.result.Result;
import com.booktrade.service.IBooksService;
import com.booktrade.service.IBooksTagsService;
import com.booktrade.service.ITagsService;
import com.booktrade.util.BaseContext;
import com.booktrade.vo.BookCategoryVo;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 书籍表，存储所有二手书籍的详细信息 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/books")
@RequiredArgsConstructor
public class BooksController {
    private final IBooksService booksService;
    private final ITagsService tagsService;
    private final IBooksTagsService booksTagsService;
    @GetMapping("/category")
    @Operation(summary = "获取书籍分类")
    public Result<List<BookCategoryVo>> getCategory() {
        List<Books> list = booksService.list(new LambdaQueryWrapper<Books>().select(Books::getCategory).groupBy(Books::getCategory));
        List<BookCategoryVo> categoryVoList = BeanUtil.copyToList(list, BookCategoryVo.class);
        return Result.success(categoryVoList);
    }

    @PostMapping("/publish")
    @Operation(summary = "发布书籍")
    public Result publishBook(@RequestBody Books books) {
        Integer userId = BaseContext.getCurrentId();
        booksService.publish(books, userId);
        return Result.success();
    }

    @GetMapping("/user")
    @Operation(summary = "获取用户书籍列表")
    public Result<List<Books>> list() {
        Integer userId = BaseContext.getCurrentId();
        List<Books> list = booksService.list(new LambdaQueryWrapper<Books>().eq(Books::getSellerId, userId));
        return Result.success(list);
    }


    @PutMapping("/update")
    @Operation(summary = "修改书籍信息")
    public Result updateBook(@RequestBody Books books) {
        Integer userId = BaseContext.getCurrentId();
        booksService.updateBook(books, userId);
        return Result.success();
    }

    @PostMapping("/list")
    @Operation(summary = "搜索书籍")
    public Result<Page<Books>> ListBooksPage(@RequestBody BookSearchDTO bookSearchDTO) {
        Page<Books> page = booksService.listBooksPage(bookSearchDTO);
        return Result.success(page);
    }
    @DeleteMapping("/{id}")
    @Operation(summary = "删除书籍")
    public Result delete(@PathVariable("id") Long id) {
        booksService.removeById(id);
        return Result.success();
    }
    @GetMapping("/{id}")
    @Operation(summary = "获取书籍详情")
    public Result<Books> getById(@PathVariable("id") Long id) {
        Books books = booksService.getById(id);
        List<BooksTags> list = booksTagsService.list(new LambdaQueryWrapper<BooksTags>().eq(BooksTags::getBookId, id));
        List<Tags> tagsList = list.stream().map(bt -> {
            Tags tag = tagsService.getById(bt.getTagId());
            return tag;
        }).collect(Collectors.toList());
        books.setTagsList(tagsList);
        return Result.success(books);
    }

    @PostMapping("/tags")
    @Operation(summary = "添加标签")
    public Result addTags(@RequestBody Tags tags) {
        tagsService.save(tags);
        return Result.success();
    }
    @DeleteMapping("/tags/{id}")
    @Operation(summary = "删除标签")
    public Result deleteTags(@PathVariable("id") Long id) {
        tagsService.removeById(id);
        return Result.success();
    }
    @GetMapping("/tags/{id}")
    @Operation(summary = "获取标签详情")
    public Result<Tags> getTagsById(@PathVariable("id") Long id) {
        Tags tags = tagsService.getById(id);
        return Result.success(tags);
    }
    @PutMapping("/tags")
    @Operation(summary = "修改标签")
    public Result updateTags(@RequestBody Tags tags) {
        tagsService.updateById(tags);
        return Result.success();
    }


}
